package com.mming.sso.bean;

import com.mming.sso.util.HTTPStatusUtil;
import lombok.Data;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;

@Data
public class ResponseWrapper {
    private int status;
    private String info;

    public ResponseWrapper(int status, String... info) {
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getResponse();

        this.status = status;
        response.setStatus(status);
        if (info.length <= 0)
            setDefaultInfoByStatus(status);
        else
            this.info = info[0];
    }

    protected void setDefaultInfoByStatus(int status) {
        switch (status) {
            case 200:
                this.info = HTTPStatusUtil.SUCCESS;
                break;
            case 400:
                this.info = HTTPStatusUtil.BAD_REQUEST;
                break;
            case 403:
                this.info = HTTPStatusUtil.FORBIDDEN;
                break;
            case 405:
                this.info = HTTPStatusUtil.METHOD_NOT_ALLOWED;
                break;
            case 500:
                this.info = HTTPStatusUtil.SERVER_ERROR;
                break;
            case 800:
                this.info = HTTPStatusUtil.USER_EXIST;
                break;
            case 801:
                this.info = HTTPStatusUtil.INVALID_USER_INFO;
                break;
        }
    }
}
